package com.ftg.learn.framework.mvc.config;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 配置类吧
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/18 上午8:42
 */
@Configuration
@ComponentScan("com.ftg.learn.framework.mvc")
public class App {


    private final String url = "jdbc:mysql://localhost:3309/j2305?timeZone=Asia/Harbin&characterEncoding=utf-8";
    private final String user = "root";
    private final String password = "123456";


    /**
     * 用来生成数据源
     *
     * @return
     */
    @Bean
    public DataSource createDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser(this.user);
        dataSource.setPassword(this.password);
        dataSource.setURL(this.url);
        return dataSource;
    }

    /**
     * 换包了  SqlSessionFactoryBean
     *
     * import org.mybatis.spring.SqlSessionFactoryBean;
     *  spring mybatis 整合的包里面提供的类
     *
     *  spring容器没有接管mybatis 的sql映射器
     *
     * @return
     */
    @Bean("sqlsessionfactorbean")
    public SqlSessionFactoryBean getSqlSession() {

        SqlSessionFactoryBean s = new SqlSessionFactoryBean();
        s.setDataSource(createDataSource());
        return s;
//        s.setMapperLocations("");
    }
//
//    @Bean
//    public MapperScannerConfigurer initAbc(){
//        MapperScannerConfigurer m = new MapperScannerConfigurer();
//        m.setSqlSessionFactoryBeanName("sqlsessionfactorbean");
//        m.setBasePackage("com.ftg.learn.framework.mvc.dao");
//        return m;
//    }

}
